package com.mystudy.management.usermanagement.controller;


import com.alibaba.fastjson.JSONObject;

import com.mystudy.management.usermanagement.laytablemessageconvert.LayuiSelectMessageConvert;
import com.mystudy.management.usermanagement.laytablemessageconvert.LayuiTableMessageConvert;
import com.mystudy.management.usermanagement.service.Index2_1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.Map;

/*
   --------------------------------------------- 访问监管控制器----------------------------------------------------------------
   */
@RestController
@RequestMapping("/admin/index/supervision/log")
public class Index2_1Controller {


    @Autowired//注入事务
    private Index2_1Service service;

    @Autowired//注入数据表返回数据打包工具
    private LayuiTableMessageConvert tableConvert;

    @Autowired//注入请求信息解包工具
    private LayuiSelectMessageConvert selectMessageConvert;

    /*
    ---------------------------------------------------访问记录---------------------------------------------------------------
     */
    //刷新数据源
    @GetMapping("/visitors")
    public JSONObject getAllLog(){
        return (JSONObject) tableConvert.index2messagePacking(service.selectAllLog());

    }

    //查询数据
    @GetMapping("/visitors/{indexkind}/{message}/{start}/{end}")
    public JSONObject getLogByConditions(
                                         @PathVariable("indexkind") String indexKind,
                                         @PathVariable("message") String message,
                                         @PathVariable("start") String startDate,
                                         @PathVariable("end") String endDate

    ){
        try{
            Map<String,Object> resultMap = selectMessageConvert.index2SelectMessageConvert(indexKind,message,startDate,endDate);
            JSONObject response;
            if(indexKind.equals("byId")){
                response =  (JSONObject)tableConvert.index2messagePacking((service.selectLogByCondition((Integer) resultMap.get("userId"),(Date) resultMap.get("startDate"),(Date)resultMap.get("endDate"))));
            }else{
                response =  (JSONObject)tableConvert.index2messagePacking((service.selectLogByCondition((String) resultMap.get("visitIP"),(Date) resultMap.get("startDate"),(Date)resultMap.get("endDate"))));
            }
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return new JSONObject();
        }

    }

    //删除数据
    @DeleteMapping("/visitors")
    public String deleteVisitTrace(@RequestBody JSONObject requestBody){
        System.out.println(requestBody);
        Map<String,Integer[]> resultResponse = service.deleteLogByConditions(selectMessageConvert.index2DeleteMessageConvert(requestBody));
        Integer[] failed = resultResponse.get("Failed");
        if(failed.length != 0 ){
            return "存在"+failed.length+"条数据删除失败！";
        }
        return "删除成功！";
    }

    /*
    ------------------------------------------------------请求记录--------------------------------------------------------
     */

    //刷新数据源
    @GetMapping("/request")
    public JSONObject getAllRequestTrace(){
        return (JSONObject) tableConvert.index2messagePacking(service.selectAllRequestLog());
    }



    //查询数据，带日期
    @GetMapping("/request/{message}/{start}/{end}")
    public JSONObject getRequestTraceLogByConditions(
            @PathVariable("message") String message,
            @PathVariable("start") String startDate,
            @PathVariable("end") String endDate

    ){
        try{
            Map<String,Object> resultMap = selectMessageConvert.index2RequestSelectMessageConvert(message,startDate,endDate);
            JSONObject response;
            response =  (JSONObject)tableConvert.index2messagePacking((service.selectRequestLogByCondition( (String) resultMap.get("traceIP"),(Date) resultMap.get("startDate"),(Date)resultMap.get("endDate"))));
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return new JSONObject();
        }

    }

    //查询数据，不带日期
    @GetMapping("/request/{message}")
    public JSONObject getRequestTraceLogWithoutConditions(
            @PathVariable("message") String message

    ){
        Map<String,Object> resultMap = selectMessageConvert.index2RequestSelectMessageConvert(message);
        JSONObject response;
        response =  (JSONObject)tableConvert.index2messagePacking((service.selectRequestLogByCondition((String) resultMap.get("traceIP"))));
        return response;

    }


    //查询数据，只有日期
    @GetMapping("/request/{start}/{end}")
    public JSONObject getRequestTraceLogWithDateRange(
            @PathVariable("start") String startDate,
            @PathVariable("end") String endDate

    ){
        try{
            Map<String,Object> resultMap = selectMessageConvert.index2RequestSelectMessageConvert(startDate,endDate);
            JSONObject response;
            response =  (JSONObject)tableConvert.index2messagePacking((service.selectRequestLogByCondition( (Date) resultMap.get("startDate"),(Date)resultMap.get("endDate"))));
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return new JSONObject();
        }

    }

    //批量删除数据
    @DeleteMapping("/request")
    public String deleteRequestTraces(@RequestBody JSONObject requestBody){
        System.out.println(requestBody);
        Map<String,Integer[]> resultResponse = service.deleteRequestLogByConditions(selectMessageConvert.index2DeleteMessageRequestTraceConvert(requestBody));
        Integer[] failed = resultResponse.get("Failed");
        if(failed.length != 0 ){
            return "存在"+failed.length+"条数据删除失败！";
        }
        return "删除成功！";
    }
}
